package com.mutual.healthInfo.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mutual.attachment.pojo.Attachment;
import com.mutual.attachment.service.AttachmentService;
import com.mutual.commom.pojo.ReturnResult;
import com.mutual.commom.util.SystemUtil;
import com.mutual.common.BaseController;
import com.mutual.common.ResultList;
import com.mutual.healthInfo.pojo.HealthInfo;
import com.mutual.healthInfo.service.HealthInfoService;
import com.mutual.product.pojo.Product;
import com.mutual.product.service.ProductService;

@Controller
@RequestMapping("/healthInfo")
public class HealthInfoController extends BaseController{

	@Resource
	private ProductService productService;
	
	@Resource
	private AttachmentService attachmentService;
	
	@Resource
	private HealthInfoService healthInfoService;
	
	
	@RequestMapping(value = "/toList")
	public String list(){
		return "healthInfo/healthInfoList";
	}
	
	@RequestMapping(value = "/listData")
	@ResponseBody
	public ResultList<HealthInfo> listData(@RequestParam Map<String,Object> param){
		total=healthInfoService.selectHealthInfoCount(param);
		ResultList<HealthInfo> result=new ResultList<>();
		result.setCount(total);
		List<HealthInfo> healthInfoList=healthInfoService.selectHealthInfoList(param);
		result.setData(healthInfoList);
		return result;
	}
	
	@RequestMapping(value = "/toAddHealthInfo")
	public String toAddProduct(){
		return "healthInfo/addHealthInfo";
	}
	
	
	@RequestMapping(value = "/addHealthInfo")
	@ResponseBody
	public ReturnResult addHealthInfo(HealthInfo healthInfo){
		ReturnResult returnResult=new ReturnResult();
		try {
////			Attachment attachment=new Attachment();
////			attachment.setId(SystemUtil.getInt(param.get("attachmentId")));
////			attachment.setType(Attachment.TYPE_PRODUCT);
//			returnResult = productService.addProduct(product, attachment);
			healthInfoService.insert(healthInfo);
			returnResult.setStatus(ReturnResult.STATUS_SUCCESS);
		} catch (Exception e) {
			returnResult.setReturnMsg("添加异常");
			returnResult.setStatus(ReturnResult.STATUS_FAILED);
		}
		return returnResult;
	}
	
	@RequestMapping(value = "/toUpdateHealthInfo")
	public String toUpdateHealthInfo(int id,Model model){
		HealthInfo healthInfo=healthInfoService.selectByPrimaryKey(id);
//		Attachment attachment=new Attachment();
//		attachment.setType(Attachment.TYPE_PRODUCT);
//		attachment.setMoudleId(id);
//		List<Attachment> attachments=attachmentService.selectAttachmentByMoudleId(attachment);
//		if(!attachments.isEmpty()){
//			model.addAttribute("attachment", attachments.get(0));
//		}
		model.addAttribute("healthInfo", healthInfo);
		return "healthInfo/updateHealthInfo";
	}
	
	@RequestMapping(value = "/toDetailHealthInfo")
	public String toDetailHealthInfo(int id,Model model){
		HealthInfo healthInfo=healthInfoService.selectByPrimaryKey(id);
//		Attachment attachment=new Attachment();
//		attachment.setType(Attachment.TYPE_PRODUCT);
//		attachment.setMoudleId(id);
//		List<Attachment> attachments=attachmentService.selectAttachmentByMoudleId(attachment);
//		if(!attachments.isEmpty()){
//			model.addAttribute("attachment", attachments.get(0));
//		}
		model.addAttribute("healthInfo", healthInfo);
		return "healthInfo/detailHealthInfo";
	}
	
	@RequestMapping(value = "/updateHealthInfo")
	@ResponseBody
	public ReturnResult updateProduct(@RequestParam Map<String,Object> param,HealthInfo healthInfo){
		ReturnResult returnResult=new ReturnResult();
		try {
//			Attachment attachment=new Attachment();
//			attachment.setId(SystemUtil.getInt(param.get("attachmentId")));
//			attachment.setType(Attachment.TYPE_PRODUCT);
//			returnResult = productService.updateProduct(product, attachment);
			healthInfoService.updateByPrimaryKey(healthInfo);
			returnResult.setStatus(ReturnResult.STATUS_SUCCESS);
		} catch (Exception e) {
			returnResult.setReturnMsg("修改异常");
			returnResult.setStatus(ReturnResult.STATUS_FAILED);
		}
		return returnResult;
	}
	
	@RequestMapping(value = "/deleteHealthInfo")
	@ResponseBody
	public boolean deleteHealthInfo(int id){
		boolean flag=false;
		try {
			healthInfoService.deleteHealthInfo(id);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
}
